package com.bw.shop_2302.fg

import android.content.Context
import android.os.Bundle
import android.os.Handler
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.bw.base.entity.Roll
import com.bw.base.mvvm.BaseFragment
import com.bw.shop_2302.R
import com.bw.shop_2302.adapter.GoodsAdapter
import com.bw.shop_2302.adapter.MenuAdapter
import com.bw.shop_2302.adapter.MoneyAdapter
import com.bw.shop_2302.adapter.NewsAdapter
import com.bw.shop_2302.databinding.FragmentHomeBinding
import com.bw.shop_2302.utils.GlideUtils
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.youth.banner.loader.ImageLoader
import java.util.Timer
import java.util.TimerTask


class HomeFragment:BaseFragment<FragmentHomeBinding,HomeViewModel>() {

    var list = mutableListOf<String>()
    lateinit var newsAdapter: NewsAdapter
    lateinit var menuAdapter: MenuAdapter
    lateinit var moneyAdapter: MoneyAdapter
    lateinit var goodsAdapter: GoodsAdapter
    lateinit var timer: Timer
    var handler=Handler()
    var position=0 //新闻列表下标
    var type=4
    var page=1
    override fun initView() {

        viewModel.index()

        //初始化菜单
        menuAdapter= MenuAdapter()
        binding.rvMenu.adapter=menuAdapter
        binding.rvMenu.layoutManager=StaggeredGridLayoutManager(5,StaggeredGridLayoutManager.VERTICAL)


        newsAdapter= NewsAdapter()
        binding.rvNews.adapter=newsAdapter
        binding.rvNews.layoutManager=LinearLayoutManager(requireContext())


        //开启定时任务,数据滚动news
        timer= Timer()

        timer.schedule(object :TimerTask(){
            override fun run() {

                handler.post {


                    binding.rvNews.scrollToPosition(position)//第一次为0
                    position++
                    if(position==newsAdapter.data.size){
                        position=0
                    }

                    //position=1
                }

            }

        },0,2000)


        //初始化省钱分类

        moneyAdapter= MoneyAdapter()
        binding.rvMoney.adapter=moneyAdapter
        binding.rvMoney.layoutManager=GridLayoutManager(context,4)

        moneyAdapter.setOnItemClickListener { adapter, view, position ->


            type=moneyAdapter.data.get(position).type.toInt()
            moneyAdapter.updatetype(type)



            //每次点击分类时，查看的数据一定是第一页
            page=1
            viewModel.goods(type,5,page)
        }

        //初始化商品列表
        goodsAdapter= GoodsAdapter()
        binding.rvGoods.adapter=goodsAdapter
        binding.rvGoods.layoutManager=GridLayoutManager(context,2)

        viewModel.goods(type,5,page)

        //点击分类时，下方数据变化，同时文字颜色变为红色


        //刷新框架

        binding.srl.setOnRefreshLoadMoreListener(object :OnRefreshLoadMoreListener{
            override fun onRefresh(refreshLayout: RefreshLayout) {

                page=1

                viewModel.goods(type,5,page)
                binding.srl.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {

                page++
                viewModel.goods(type,5,page)
                binding.srl.finishLoadMore()
            }

        })

    }

    override fun initData() {

        viewModel.indexsuccess.observe(this){

            if(it.code==200){

                //轮播图
                it.data.banner.forEach {
                    list.add(it.pic)
                }



                binding.banner.setImages(list)
                binding.banner.setImageLoader(object :ImageLoader(){
                    override fun displayImage(
                        context: Context,
                        path: Any,
                        imageView: ImageView
                    ) {
                       GlideUtils.normalimag(context,path.toString(),imageView)
                    }

                }).start()


                //菜单
                menuAdapter.data.clear()
                menuAdapter.data.addAll(it.data.menus)
                menuAdapter.notifyDataSetChanged()


                //新闻
                newsAdapter.data.clear()
                newsAdapter.data.addAll(it.data.roll)
                newsAdapter.data.add(Roll(123,"后天放假","",""))
                newsAdapter.notifyDataSetChanged()



                //省钱分类
                moneyAdapter.data.clear()
                moneyAdapter.data.addAll(it.data.explosiveMoney)
                moneyAdapter.notifyDataSetChanged()


                //商品





            }else{

                ToastUtils.showLong(it.message)

            }

            viewModel.goodssuccess.observe(this){

                if(it.code==200){

                    if(page==1){
                        goodsAdapter.data.clear()
                    }


                    goodsAdapter.data.addAll(it.data.list)
                    goodsAdapter.notifyDataSetChanged()

                    //当没有请求到数据是
                    if(it.data.list.isEmpty()){
                        ToastUtils.showLong("没有加载到更多数据")
                    }
                }

            }

        }

    }

    override fun getLayout(): Int = R.layout.fragment_home

    override fun getViewModel(): Class<HomeViewModel> = HomeViewModel::class.java


}